package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Arrays;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealControlller {

    @Autowired
    JedisPool jedisPool;

    @Reference
    SetmealService setmealService;

    @RequestMapping("/delById")
    public Result delById(Integer id){
        setmealService.delById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }
    @RequestMapping("/edit")
    public Result edit(Integer[]checkgroupIds,@RequestBody Setmeal setmeal){
        setmealService.edit(checkgroupIds,setmeal);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @RequestMapping("/getToken")
    public Result getToken(){
        String ak = "_E6hY6otHUkNMpJAVf_32i2SU7GxEfUe8s3f8hyM";
        String sk = "e-aaythGI0AoeAHwlrYnbERKzpAeO3jshUndyGr0";
        Auth auth = Auth.create(ak ,sk);
        String token = auth.uploadToken("itcasthealth359");
        log.debug("套餐图片上传生成token成功！！");
        return new Result(true, MessageConst.SETMEAL_GET_TOKEN, token);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        log.debug("SetmealControlller:add:" + setmeal);
        log.debug("SetmealControlller:add:" + Arrays.toString(checkgroupIds));
        setmealService.add(setmeal, checkgroupIds);
        log.debug("套餐添加成功！！");
        return  new Result(true ,MessageConst.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("SetmealControlller:findPage:" + queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        log.debug("套餐查询成功！！");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS, pageResult);
    }

    @RequestMapping("/addImgName")
    public Result addImgName(String imgName){
        log.debug("SetmealControlller:addImgName:" + imgName);
        jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_RESOURCES, imgName);
        log.debug("图片名称添加到redis成功！！");
        return new Result(true,MessageConst.ADD_IMGNAME_REDIS);
    }
}
